var mysql=require('mysql');
var conf=require('../config/db.js');
var util=require('../util/util.js');
//使用连接池
var pool  = mysql.createPool(util.extend({}, conf.mysql_z));

class DataSource {
    constructor(){
        this.pool = pool;
    }

    jsonWrite(res, ret){
        if(typeof ret=='undefind'){
            res.json({
                code: 1,
                msg:'操作失败'
            });
        }else{
            res.json({
                code: 0,
                data: ret
            });
        }
    }
    query(sql, callback) {
        pool.getConnection(function(err,conn){
            if(err){
                callback(err,null,null);
            }else{
                conn.query(sql,function(qerr,vals,fields){
                    //释放连接
                    conn.release();
                    //事件驱动回调
                    callback(qerr,vals,fields);
                });
            }
        });
    };

}


module.exports = new DataSource();